package com.bsoft.gol.hcs.clinical.dao;

import com.bsoft.gol.hcs.clinical.bean.qo.CommonPrescriptionQo;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import com.bsoft.gol.hcs.clinical.entity.OdsCommonPrescriptionInfo;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class OdsCommonPrescriptionInfoDAO extends HibernateSupportDelegateDAO<OdsCommonPrescriptionInfo> {
    public OdsCommonPrescriptionInfoDAO() {
        this.setKeyField("commonPrescriptionInfoId");
    }

    @DAOMethod(sql = "update OdsCommonPrescriptionInfo set deleteFlag='1',deleteUser=:deleteUser,deleteTime=now() where commonPrescriptionInfoId in (:commonPrescriptionInfoIds)")
    public abstract void updatePrescriptionStatusByIds(@DAOParam("commonPrescriptionInfoIds") List<String> commonPrescriptionInfoIds,@DAOParam("deleteUser") String deleteUser);

    @DAOMethod(sql = "update OdsCommonPrescriptionInfo set deleteFlag='1',deleteUser=:deleteUser,deleteTime=now() where commonPrescriptionInfoId =:commonPrescriptionInfoId")
    public abstract void updatePrescriptionStatusById(@DAOParam("commonPrescriptionInfoId") String commonPrescriptionInfoId,@DAOParam("deleteUser") String deleteUser);

    @DAOMethod(sql = "update OdsCommonPrescriptionInfo set numberOfPackets=:numberOfPackets,boilSign=:boilSign where commonPrescriptionInfoId =:commonPrescriptionInfoId")
    public abstract void updatePrescriptionByIdAndDrug(@DAOParam("commonPrescriptionInfoId")String commonPrescriptionInfoId,
                                                       @DAOParam("numberOfPackets")Integer numberOfPackets,
                                                       @DAOParam("boilSign")String boilSign);

    @DAOMethod(sql = " from OdsCommonPrescriptionInfo  where commonPrescriptionName =:commonPrescriptionName and doctorId=:doctorId and deleteFlag='0'")
    public abstract List<OdsCommonPrescriptionInfo> findCommonPrescriptionByName(@DAOParam("commonPrescriptionName") String commonPrescriptionName,@DAOParam("doctorId") String doctorId);

@DAOMethod(sql = " from OdsCommonPrescriptionInfo  where commonPrescriptionName =:commonPrescriptionName and doctorId=:doctorId and deleteFlag='0' and commonPrescriptionInfoId !=:commonPrescriptionInfoId")
    public abstract List<OdsCommonPrescriptionInfo> findPrescriptionByName(@DAOParam("commonPrescriptionName") String commonPrescriptionName,@DAOParam("doctorId") String doctorId,@DAOParam("commonPrescriptionInfoId") String commonPrescriptionInfoId);

    public List<OdsCommonPrescriptionInfo> queryPrescriptionByDoctorId(final CommonPrescriptionQo qo) {
        HibernateStatelessResultAction<List<OdsCommonPrescriptionInfo>> action = new AbstractHibernateStatelessResultAction<List<OdsCommonPrescriptionInfo>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("select common_prescription_info_id as commonPrescriptionInfoId,common_prescription_name as commonPrescriptionName,");
                sql.append("org_id as orgId,doctor_id as doctorId,number_of_packets as numberOfPackets,boil_sign as boilSign,prescription_type as prescriptionType ");
                sql.append("from ods_common_prescription_info where delete_flag='0' and doctor_id=:doctorId ");
                if(StringUtils.isNotEmpty(qo.getCommonPrescriptionName())){
                    sql.append("and common_prescription_name like '%").append(qo.getCommonPrescriptionName()).append("%'");
                }
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("doctorId", qo.getDoctorId());
                int first = qo.getPageNo() > 0 ? (qo.getPageNo() - 1) * qo.getPageSize() : 0;
                query.setFirstResult(first);
                query.setMaxResults(qo.getPageSize());
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<OdsCommonPrescriptionInfo> prescriptionList = new ArrayList<OdsCommonPrescriptionInfo>();
                List<Map> list = query.list();
                if (list.size() > 0) {
                    for (Map map : list) {
                        prescriptionList.add(ConversionUtils.convert(map, OdsCommonPrescriptionInfo.class));
                    }
                }
                setResult(prescriptionList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}